# -*- coding: utf-8 -*-
import time
from .blacklist import BlackList
from .magic_mirror import MagicMirror
from .ecommerce_brain import ECommerceBrain


class UserCheck:
    env_ok = False
    brain = None
    mirror = None
    blacklist = None

    def __init__(self, config, print_log, no_log=False):
        self.accounts = config.account
        self.platform = config.platform
        self.debug = config.debug
        self.no_log = no_log
        self.print_log = print_log
        self.print_log("开始初始化验号环境...", debug=self.no_log)
        start = time.time()
        if self.init_env():
            self.print_log(f"验号环境初始化成功.共用时: {time.time()-start:.2f}s\n", debug=self.no_log)
        else:
            self.print_log("验号环境初始化失败，请重启软件！\n", debug=self.no_log)

    def init_env(self):
        if self.platform.ecommerce_brain:
            name, pwd = self.accounts.ecommerce_brain
            self.brain = ECommerceBrain(name, pwd, self.print_log, self.debug.ecommerce_brain, self.no_log)
            if not self.brain.env_ok:
                return False

        if self.platform.magic_mirror:
            name, pwd = self.accounts.magic_mirror
            self.mirror = MagicMirror(name, pwd, self.print_log, self.debug.magic_mirror, self.no_log)
            if not self.mirror.env_ok:
                return False

        if self.platform.blacklist:
            self.blacklist = BlackList(self.print_log, self.no_log)
            if not self.blacklist.env_ok:
                return False

        self.env_ok = True
        return True

    def check_user(self, username, print_log, shop="无数据"):
        if not self.env_ok:
            print_log("验号环境初始化失败，请重启软件！", debug=self.no_log)
            return None
        if username.strip() == "":
            return False

        if self.platform.blacklist:
            print_log("开始黑名单验号...")
            if self.blacklist.check_user(username, shop):
                print_log("黑名单验号结果: 通过")
            else:
                print_log("黑名单验号结果: 不通过")
                return False

        if self.platform.ecommerce_brain:
            print_log("开始电商大脑验号...")
            user = self.brain.search_user(username)
            if user:
                print_log(user)
            else:
                print_log("用户不存在或出错！")
            if self.brain.check(user):
                print_log("电商大脑验号结果: 通过")
            else:
                print_log("电商大脑验号结果: 不通过")
                if self.platform.blacklist:
                    self.print_log(f"添加用户【{username}】到黑名单.")
                    self.blacklist.add_blacklist(username)
                return False

        if self.platform.magic_mirror:
            print_log("开始照妖镜验号...")
            user = self.mirror.search_user(username)
            if user:
                print_log(user)

            if self.mirror.check(user):
                print_log("照妖镜验号结果: 通过")
            else:
                print_log("照妖镜验号结果: 不通过")
                if self.platform.blacklist:
                    self.print_log(f"添加用户【{username}】到黑名单.")
                    self.blacklist.add_blacklist(username)
                return False

        if self.platform.blacklist:
            self.print_log(f"添加用户【{username}】到已刷名单.")
            self.blacklist.add_used(username, shop)

        return True

    def get_memory(self):
        memory = 0
        if self.platform.ecommerce_brain and self.brain:
            memory += self.brain.get_memory()
        if self.platform.magic_mirror and self.mirror:
            memory += self.mirror.get_memory()
        return memory

    def quit(self):
        self.brain.quit()
        self.mirror.quit()


if __name__ == "__main__":
    from ..tools import Config
    check = UserCheck(Config())
    while True:
        user = input("输入用户名：")
        start = time.time()
        result = check.check_user(user, print_log=print)
        print(f"time: {time.time()-start}")
        print(f"验号结果: {'通过' if result else '不通过'}")
